package fr.pud.client.controller.bubble;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;

import fr.pud.client.model.PUDStation;
import fr.pud.client.view.JMapViewerExtended;
import fr.pud.client.view.mapstation.Bubble;
import fr.pud.client.view.mapstation.BubbleBackground;
import fr.pud.client.view.mapstation.BubbleSubBackground;

/**
 * This class implements a MousListener which enables to
 * display the address depending of the action of the mouse on the
 * address pointer
 * Apr 8, 2012 - OSE
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         ShowBubble.java
 */
public class ShowBubble extends MouseAdapter {
    /**
     * The constants used to place the bubble on the right place.
     */
    private static final int MOVE_RIGHT      = 1;
    /**
     * The constants used to place the bubble on the right place.
     */
    private static final int MOVE_LEFT       = 2;
    /**
     * The constants used to place the bubble on the right place.
     */
    private static final int MOVE_DOWN       = 3;
    /**
     * The constants used to place the bubble on the right place.
     */
    private static final int MOVE_DOWN_RIGHT = MOVE_DOWN + MOVE_RIGHT;
    /**
     * The constants used to place the bubble on the right place.
     */
    private static final int MOVE_DOWN_LEFT  = MOVE_DOWN + MOVE_LEFT;
    /**
     * The constants used to place the bubble on the right place.
     */
    private static int       GET_SUB_BUBBLE  = 0;
    /**
     * The button which enables to show the bubble.
     */
    private JButton          pointer;
    /**
     * The station linked to the bubble.
     */
    private PUDStation       station;
    /**
     * The value which enables to know in which case we are.
     */
    private int              switchCase;

    /**
     * The constructor to sets up the data
     * @param pointer
     *            the button to link
     * @param station
     *            the station to link
     * @param vehicle
     *            the vehicle to link
     */
    public ShowBubble(JButton pointer, PUDStation station) {
        this.pointer = pointer;
        this.station = station;
        this.switchCase = 0;
    }

    /**
     * @param anInt
     *            add anInt to the current value of the switch case.
     */
    private void addSwitchcase(int anInt) {
        this.switchCase += anInt;
    }

    /**
     * Calculates the location of the bubble
     */
    private void changeBubbleAccordingToPointer() {
        if (this.pointer.getLocation().x < Bubble.getWidth()) {
            this.addSwitchcase(MOVE_RIGHT);
        }
        if (this.pointer.getLocation().x > Toolkit.getDefaultToolkit()
                .getScreenSize().getWidth()
                - Bubble.getWidth()) {
            this.addSwitchcase(MOVE_LEFT);
        }
        if (this.pointer.getLocation().y < Bubble.getHeight()) {
            this.addSwitchcase(MOVE_DOWN);
        }
    }

    /**
     * @return the value of the case according to the location.
     */
    private int getSwitchCase() {
        return this.switchCase;
    }

    /**
     * Put the switch case to 0
     */
    private void initSwitchcase() {
        this.switchCase = 0;
    }

    /**
     * The action when the button is clicked (show the bubble...)
     */
    @Override
    public void mouseClicked(MouseEvent e) {
        this.changeBubbleAccordingToPointer();
        Dimension dimToMoveDimension = this.setGetSubBubble();
        int x = this.pointer.getLocation().x - Bubble.getWidth() / 2 + 55
                - (int) dimToMoveDimension.getWidth();
        int y = this.pointer.getLocation().y - Bubble.getHeight()
                - (int) dimToMoveDimension.getHeight();
        Bubble.getBubble(this.station, GET_SUB_BUBBLE).setBounds(x, y,
                Bubble.getWidth(), Bubble.getHeight());
        Bubble.getBubble(this.station, GET_SUB_BUBBLE).setVisible(true);
        // To delete if the program comes across difficulties to repaint
        JMapViewerExtended.getJMapViewer().repaint(
                Bubble.getBubble(this.station, GET_SUB_BUBBLE).getBounds());
        this.initSwitchcase();
        JMapViewerExtended.getJMapViewer().setMapMarkerVisible(false);
    }

    /**
     * @return the dimension of the sub-bubble to be sure that the location
     *         is correct.
     */
    private Dimension setGetSubBubble() {
        switch (this.getSwitchCase()) {
            case MOVE_RIGHT:
                GET_SUB_BUBBLE = BubbleSubBackground.LEFT_SUB_BUBBLE;
                return new Dimension(-BubbleBackground.getWidth() / 2 + 45, -5);
            case MOVE_LEFT:
                GET_SUB_BUBBLE = BubbleSubBackground.RIGHT_SUB_BUBBLE;
                return new Dimension(BubbleBackground.getWidth() / 2 + 45, -5);
            case MOVE_DOWN_RIGHT:
                GET_SUB_BUBBLE = BubbleSubBackground.TOP_LEFT_SUB_BUBBLE;
                return new Dimension(-BubbleBackground.getWidth() / 2 + 45,
                        -Bubble.getHeight() - 11);
            case MOVE_DOWN_LEFT:
                GET_SUB_BUBBLE = BubbleSubBackground.TOP_RIGHT_SUB_BUBBLE;
                return new Dimension(BubbleBackground.getWidth() / 2 + 42,
                        -Bubble.getHeight() - 13);
            case MOVE_DOWN:
                GET_SUB_BUBBLE = BubbleSubBackground.CENTER_TOP_SUB_BUBBLE;
                return new Dimension(95, -Bubble.getHeight() - 17);
            default:
                GET_SUB_BUBBLE = BubbleSubBackground.CENTER_SUB_BUBBLE;
                return new Dimension(0, 0);
        }
    }
}
